<?
layout("/inc/layout.html",{title:sysTitle}){
?>

<div id="page-wrapper">
	<!--BEGIN TITLE & BREADCRUMB PAGE-->
	<div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">
		<div class="page-header pull-left">
			<div class="page-title">部门管理</div>
		</div>
		<ol class="breadcrumb page-breadcrumb pull-left">
			<li><i class="fa fa-home"></i>&nbsp;<a href="main.html">首页</a>&nbsp;&nbsp;<i
				class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
			<li>系统管理&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
			<li class="active">部门管理</li>
		</ol>

		<div class="clearfix"></div>
	</div>
	<!--END TITLE & BREADCRUMB PAGE-->
	<!--BEGIN CONTENT-->
	<div class="page-content">


		<div class="row mbl" >
			<div class="col-lg-4">
	            <div class="panel" style="height: 540px; overflow: auto;">
	                <div class="panel-heading">部门列表</div>
	                <div class="panel-body" style="padding-top: 8%;">
	                	<ul id="tree" class="ztree" style="width:270px"; overflow:auto;"></ul>
	                </div>
	            </div>
	        </div>
			 <div class="col-lg-4">
				<div class="panel" style="height: 540px">
					<div class="panel-heading">部门信息</div>
					<div class="form-body pal">
					<div style="padding-top: 5%; margin-right: auto; padding-bottom: 65px;">
					<label for="inputUsername" class="col-md-3 control-label">
					  <div align="right">部门名称
						<span class='require' id="bianhao">*</span></div>
                                      </label>
					<div class="col-md-7">
						<div class="input-icon">
							<input id="name" type="text" class="form-control" />
							<input id="id" type="hidden" class="form-control" />
						</div>
					</div>
				</div>

				<div style="margin-right: auto; padding-bottom: 65px;">
					<label for="inputUsername" class="col-md-3 control-label">
					  <div align="right">部门描述
						<span class='require' id="paixu">*</span></div>
                                      </label>
					<div class="col-md-7">
						<div class="input-icon">
							<input id="description" type="text" class="form-control" />
						</div>
					</div>
				</div>
						
				<div style="margin-right: auto; padding-bottom: 60px;">
					<label for="inputUsername" class="col-md-3 control-label">
					  <div align="right">父节点
						<span class='require' id="fuji"></span></div>
                                      </label>
					<div class="col-md-7">
						<div class="input-group input-group-sm mbs" >
							 <input id="parentclassname" type="text"  value="根目录" class="form-control" disabled="true" /> 
							 <input id="parentClassid" type="hidden" value="0" class="form-control" />
							<span class="input-group-btn"><button id="xuanze" onclick="saveclassZTree()" type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle">选择类..</button></span>
						</div>
					</div>
				</div>
						
				<div style="text-align: center; padding-bottom: 60px;">
					<input class="btn btn-blue btn-square" type="button" onclick="editUserGroup()" value="保存"/>&nbsp;&nbsp;&nbsp;
					<input class="btn btn-blue btn-square" type="button" onclick="emptyUserGroup()" value="清空"/>&nbsp;&nbsp;&nbsp;
					<button class="btn btn-blue btn-square" type="button"  onclick="deleteUserGroup()" >删除</button>
               		
				</div>
					</div>
				</div>
			</div>
	        
	        <div class="col-lg-4">
	            <div class="panel" style="height: 540px; overflow: auto;">
	                <div class="panel-heading">用户列表</div>
	                <div class="panel-body" >
	                <div id="menuButtion" style="padding-top: 5%;padding-left: 4%">
	                	<span class='subtitle' style="color: #CCC;" >数据为空！请选择部门列表</span>
	                </div>
	                </div>
	            </div>
	        </div>
		</div>

	<!--END CONTENT-->
</div>
</div>

<?} ?>
<script>
	var id;
	var ztreeId;
	var parentname =1;
	function getlay() {
		return id;
	}
	var userId;
	function getUserId() {
		return userId;
	}
	$(function() {
		initTree();
	});
	function initTree() {
		$.ajax({
			type : "post",
			data : {},
			url : "knowledgeEditDataQueryDepartment.do",
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : drawTree,
		});
	}

	function drawTree(data) {
	//alert(JSON.stringify(data));
		var setting = {
			view : {
				dblClickExpand : false,
			},
			data : {
				simpleData : {
					enable : true,
				}
			},
			check : {
				enable : false
			},
			callback : {
				onClick : onClick
			},
		};
		var zNodes = eval(data);
		$.fn.zTree.init($("#tree"), setting, zNodes).expandAll(true);
		zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.expandNode(zTree.getNodes()[0], true);

	}
	
	function onClick(e, treeId, treeNode) {
	 if(treeNode.children==null){
       		parentname=0;
       	 }else{
         	parentname=1;
        }
		ztreeId = treeNode.id;	
		  $.ajax({
			type : "post",
			data : {
				"id":ztreeId
			},
			url : "knowledgeEditDataRoleQueryDepartmentMessage.do",
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data){
			//	alert(JSON.stringify(data));
				 $("#name").val(data[0].name);
				 $("#id").val(data[0].id);
           		 $("#description").val(data[0].description);
           		 $("#parentclassname").val(data[0].parentName);
           		 $("#parentClassid").val(data[0].parentId);
			},
		});
		queryUser();
	}
	
	//选择父类
		function saveclassZTree() {
		layer.open({
			type : 2,
			area : [ '360px', '400px' ],
			fix : false, //不固定
			scrollbar : false,
			shadeClose : true,
			move : false,
			title:"选择父节点",
			skin : 'layui-layer-rim', //加上边框
			btn : [ '保存 ', '关闭' ],
			content : ["knowledge-edit-data-department-zTree.html", 'no'],
			yes : function(index,layer) {		
			 var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			  			  iframeWin.save();
			},
			cancel : function(index) {
			}
		});
	}
	//保存
	function editUserGroup(){
		var name=$("#name").val();
		var id=$("#id").val();
		var description=$("#description").val();
		var parentClassid=$("#parentClassid").val();
	if( name==""){
			layer.msg('请输入部门名！');
		}else if(description ==""){
			layer.msg('请输入部门描述！');
		}else{
		 $.ajax({
				type : "post",
				data : {
					"id":id,
					"name":name,
					"description":description,
					"parentClassid":parentClassid
				},
				url : "knowledgeEditDataEditDepartment.do",
				dataType : "json",
				beforeSend : function(XMLHttpRequest) {
				},
				success : function(data){
				initTree();
				layer.msg('保存成功！');
				},
			});
		}
	}
	
	//清空
	function emptyUserGroup(){
			$("#name").val("");
			$("#id").val("");
			$("#description").val("");
			$("#parentclassname").val("根目录");
			$("#parentClassid").val("0");
			layer.msg('清空成功！');
	}
	//删除
	function deleteUserGroup(){
		if(ztreeId ==null){
				layer.msg('请选择要删除的部门！');
			}else{
			if (parentname == 0) {
				if (confirm("是否确认删除！！")){
					 $.ajax({
						type : "post",
						data : {
							"id":ztreeId,
						},
						url : "knowledgeEditDataDeleteDepartment.do",
						dataType : "json",
						beforeSend : function(XMLHttpRequest) {
						},
						success : function(data){
							initTree();
							emptyUserGroup()
							if(data != 0){
								layer.msg('删除失败！请检查其他文件是否存在该部门！');
							}else{
								layer.msg('删除成功！');
							}
							
						},
					});
					/* location.href="knowledgeEditDataDeleteDepartment.do?id="+ztreeId;  */
				}
			} else {
			alert("有子分类不允许删除");
		}
		}
	}
	
	
	//添加部门
	function addUserGroup() {
		layer.open({
			type : 2,
			area : [ '650px', '650px' ],
		    scrollbar: false,
		    shadeClose: true,
			fix : false, //不固定
			title : '添加用户组',
			//maxmin : true,  
			skin: 'layui-layer-rim', //加上边框
    	    btn: ['保存 ', '关闭'],
			content : ["knowledge-edit-data-adddepartment.html",'no'],
			yes: function(index,layer){
	                var iframeWin = parent.window[layer.find('iframe')[0]['name']];
	                 iframeWin.save();
	                 initTree();
	         },
			cancel : function(index) {
			}
			
		});
	}
	//新增用户
	function addUser(lid) {
		id = lid;
		layer.open({
			type : 2,
			area : [ '400px', '400px' ],
		    scrollbar: false,
		    shadeClose: true,
			fix : false, //不固定
			title : '用户组管理',
			//maxmin : true,  
			skin: 'layui-layer-rim', //加上边框
    	    btn: ['保存 ', '关闭'],
			content : ["knowledge-edit-data-addrole.html",'no'],
			yes: function(index,layer){
	                var iframeWin = parent.window[layer.find('iframe')[0]['name']];
	                 iframeWin.save();
	         },
			cancel : function(index) {
			}
			
		});
	}
	
	//查询用户
	function queryUser(){
		$.ajax({
			type : "post",
			url : "knowledgeEditDataRoleQueryDepartmentUser.do",
			dataType : "json",
			data :{
				"id" : ztreeId
			},
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data){
				//alert(JSON.stringify(data));
				var str = "";
				$("#menuButtion").html("");
				for ( var i = 0; i < data.length; i++) {
					str += "<input style='margin-top: 10px;margin-left:6px;' class='btn btn-blue btn-square' onclick='queryUserDetails("+data[i].id+")' type='button' value="+data[i].name+" />";
				}
				$("#menuButtion").append(str);
			},
		});
	}
	
	function queryUserDetails(queryUserDetailsId){
		userId = queryUserDetailsId;
		layer.open({
			type : 2,
			area : [ '820px', '600px'  ],
			fix : false, //不固定
			scrollbar : false,
			shadeClose : true,
			move : false,
			title:"用户详情",
			skin : 'layui-layer-rim', //加上边框
			btn : ['关闭'],
			content : ["knowledge-edit-data-queryuserdetails.html", 'no'],
			yes : function(index,layer) {		
			 var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			  			  iframeWin.save();
			},
			cancel : function(index) {
			}
		});
		
	}
	
</script>